package cn.edu.cuit.service.impl;

import cn.edu.cuit.common.base.LayuiRespResult;
import cn.edu.cuit.common.base.RespResult;
import cn.edu.cuit.common.util.DCMd5Util;
import cn.edu.cuit.domain.dto.LoginFormParamDTO;
import cn.edu.cuit.domain.entity.SysUser;
import cn.edu.cuit.domain.vo.SysUserInfoVO;
import cn.edu.cuit.domain.vo.SysUserVO;
import cn.edu.cuit.mapper.SysUserMapper;
import cn.edu.cuit.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Service业务逻辑处理层：用于处理相关业务，如：日志、事务、安全....
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public RespResult login(LoginFormParamDTO loginFormParamDTO) {
        if(!"b4db".equals(loginFormParamDTO.getVercode())){
             return RespResult.error("验证码不正确");
        }
        SysUser sysUser = sysUserMapper.findSysUserByUsername(loginFormParamDTO.getUsername());
        if(sysUser != null && sysUser.getPassword().equals(DCMd5Util.getMd5(loginFormParamDTO.getPassword(),sysUser.getSalt()))){
            // 登录后应该将该账户信息进行脱敏，返回给客户端。
            SysUserInfoVO sysUserInfoVO = new SysUserInfoVO();
            sysUserInfoVO.setId(sysUser.getId());
            sysUserInfoVO.setUsername(sysUser.getUsername());
            sysUserInfoVO.setNickname(sysUser.getNickname());
            sysUserInfoVO.setPicture(sysUser.getPicture());
            return RespResult.success("登录成功",sysUserInfoVO);
        }
        return RespResult.error("用户名或密码有误");
    }

    @Override
    public LayuiRespResult findSysUserAll() {
        List<SysUserVO> sysUserList = sysUserMapper.querySysUserList();
        return LayuiRespResult.success(10,sysUserList);
    }

}
